﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using ImageRecognition2.Forms;
using LowLevelGraphics;
using LowLevelGraphics.Extractors;

namespace ImageRecognition2.Action
{
    /// <summary>
    /// Impainter. Use this to restore / replace parts in a pic by using a mask.
    /// </summary>
    public class ActionImpainter : AbstractCommand
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ActionImpainter"/> class.
        /// </summary>
        public ActionImpainter()
        {
        }

        /// <summary>
        /// Runs the specified sender.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public override void Run(object sender, EventArgs e)
        {
            Image mainFormSingletonActivePictureBoxBackgroundImage = MainFormSingleton.ActivePictureBox.BackgroundImage;
            FormChildList childList = new FormChildList();
            if (childList.ShowDialog() != DialogResult.OK) return;

            FormItem item = childList.FormItem;
            if (item == null) return;

            Image image = item.Form.pictureBox1.BackgroundImage;

            if (image == mainFormSingletonActivePictureBoxBackgroundImage)
            {
                MessageBox.Show("Please choose different pictures", "Result");
                return;
            }

            UnsafeBitmap bitmapSource = mainFormSingletonActivePictureBoxBackgroundImage;
            UnsafeBitmap imageSource = image;

            Impainter impainter = new Impainter();

            if (imageSource != null)
            {
                impainter.Restore(bitmapSource, imageSource);
            }
            else
            {
                impainter.Restore(bitmapSource);
            }

            imageSource.Dispose();
            bitmapSource.Dispose();

            MainFormSingleton.ActivePictureBox.BackgroundImage = bitmapSource.InternalBitmap;
            MainFormSingleton.ActivePictureBox.Invalidate();

        }
    }
}
